package com.bandit.controller.front;

import com.bandit.pojo.Message;
import com.bandit.pojo.User;
import com.bandit.service.MessageService;
import com.bandit.utils.Constants;
import com.bandit.utils.IpUtil;
import com.bandit.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Slf4j
public class IndexFrontController {

    @Autowired
    MessageService messageService;

    @Autowired
    RedisUtil redisUtil;


    @GetMapping({"/index","/"})
    public String list(ModelMap map){
        log.info("首页查询所有List返回给前端");    //后期需修改查前六条
        List<Message> NewsList = messageService.selectMessageSixList(new Message(Constants.MSG_TYPE_NEWS));
        List<Message> JobsList = messageService.selectMessageSixList(new Message(Constants.MSG_TYPE_JOBS));
        List<Message> MajorList = messageService.selectMessageSixList(new Message(Constants.MSG_TYPE_MAJOR));
        List<Message> NoticeList = messageService.selectMessageSixList(new Message(Constants.MSG_TYPE_NOTICES));
        map.put("NewsList",NewsList);
        map.put("JobsList",JobsList);
        map.put("MajorList",MajorList);
        map.put("NoticeList",NoticeList);
        return "index";
    }

    //去登录页面
    @GetMapping("/login")
    public String toLogin(){
        return "login";
    }

    //注销
    @GetMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:index";
    }

    @RequestMapping("/noPermission")
    public String toNoPermission(){
        return "exception/noPermission";
    }

    @RequestMapping("/err")
    public String toError(){
        return "error/error";
    }

    //登录验证的方法
    @PostMapping("/LoginConfirm")
    public String login(String username, String password, Model model , HttpServletRequest req) {
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //没有认证过
        //封装用户的登录数据,获得令牌
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //登录 及 异常处理
        try {
            //用户登录
            subject.login(token);
            Session subjectSession = subject.getSession();
            model.addAttribute("loginUser",subjectSession.getAttribute("loginUser"));
            //Redis记录访问次数
            String ipAddr = IpUtil.getIpAddr(req);
            String key = ipAddr+":"+username;
            if(redisUtil.get(key)==null){
                redisUtil.set(key,1);
            }else {
                redisUtil.incr(key,1);
            }
            return "redirect:index";
        } catch (UnknownAccountException uae) {
            log.warn("用户名不存在");
            //如果用户名不存在
            System.out.println("用户名不存在");
            model.addAttribute("exception", "用户名不存在");
            return "redirect:login";
        } catch (IncorrectCredentialsException ice) {
            log.warn("密码错误");
            //如果密码错误
            System.out.println("密码错误");
            model.addAttribute("exception", "密码错误");
            return "redirect:login";
        }

    }


}
